#include<stdio.h>
#include<time.h>
#include "array_and_file.c"
#include "swap.c"
#define MAX 50000

void insertion_sort (int *array, int array_size);
void PrintStringWithDelay (char *String, int Delay);
void ReadSortWrite (char *InputFile, char *OutputFile, int FileSize);

int main()
{
	int i, sorting_array[MAX] = { 0 };
	char end_op[] = "\n\n\t-------------END OF OPERATIONS--------------", start_op[] = "\n\n\t-----------COMMENCING OPERATIONS------------";;
	clock_t start, now;
	double elapsed_time_read, elapsed_time_write, elapsed_time_sort;

    PrintStringWithDelay(start_op, 10);

  //  ReadSortWrite ("rnd0.txt", "insertion_sorted0.txt", 8334);
  //  ReadSortWrite ("m01.txt", "insertion_sorted1.txt", 16668);

    /*******FILE 0 SIZE = 8334 ELEMENTS*******/
	printf("\n\n\n");
	printf("\tPROCESSING FILE 0, SIZE = %d\n", 8334);

    start = clock ();
    read_file_into_array (sorting_array, 8334, "rnd0.txt");     //FILE READ
    now = clock ();
	elapsed_time_read = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent reading elements from input file 0 into RAM: %6.4fs\n", elapsed_time_read);

	start = clock ();
    insertion_sort (sorting_array, 8334);       //SORT
    now = clock ();
	elapsed_time_sort = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent sorting elements in RAM: %6.4fs\n", elapsed_time_sort);

    start = clock ();
    write_array_to_file (sorting_array, 8334, "insertion_sorted0.txt");   //FILE WRITE
    now = clock ();
	elapsed_time_write = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent writing elements in RAM to output file 0: %6.4fs\n", elapsed_time_write);

	printf("\tTOTAL FILE I/O TIME = %6.4fs\n", elapsed_time_read + elapsed_time_write);
	/*******END OF FILE 0 PROCESSING*******/


	/*******FILE 1 SIZE = 16668 ELEMENTS*******/
	printf("\n\n\n");
    printf("\tPROCESSING FILE 1, SIZE = %d\n", 16668);

    start = clock ();
    read_file_into_array (sorting_array, 16668, "m01.txt");     //FILE READ
    now = clock ();
	elapsed_time_read = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent reading elements from input file 1 into RAM: %6.4fs\n", elapsed_time_read);

	start = clock ();
    insertion_sort (sorting_array, 16668);       //SORT
    now = clock ();
	elapsed_time_sort = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent sorting elements in RAM: %6.4fs\n", elapsed_time_sort);

    start = clock ();
    write_array_to_file (sorting_array, 16668, "insertion_sorted1.txt");   //FILE WRITE
    now = clock ();
	elapsed_time_write = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent writing elements in RAM to output file 1: %6.4fs\n", elapsed_time_write);

	printf("\tTOTAL FILE I/O TIME = %6.4fs\n", elapsed_time_read + elapsed_time_write);
	/*******END OF FILE 1 PROCESSING*******/


    /*******FILE 2 SIZE = 33334 ELEMENTS*******/
	printf("\n\n\n");
    printf("\tPROCESSING FILE 2, SIZE = %d\n", 33334);

    start = clock ();
    read_file_into_array (sorting_array, 33334, "m0123.txt");     //FILE READ
    now = clock ();
	elapsed_time_read = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent reading elements from input file 2 into RAM: %6.4fs\n", elapsed_time_read);

	start = clock ();
    insertion_sort (sorting_array, 33334);       //SORT
    now = clock ();
	elapsed_time_sort = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent sorting elements in RAM: %6.4fs\n", elapsed_time_sort);

    start = clock ();
    write_array_to_file (sorting_array, 33334, "insertion_sorted2.txt");   //FILE WRITE
    now = clock ();
	elapsed_time_write = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent writing elements in RAM to output file 2: %6.4fs\n", elapsed_time_write);

	printf("\tTOTAL FILE I/O TIME = %6.4fs\n", elapsed_time_read + elapsed_time_write);
	/*******END OF FILE 2 PROCESSING*******/


	/*******FILE 3 SIZE = 50000 ELEMENTS*******/
	printf("\n\n\n");
    printf("\tPROCESSING FILE 3, SIZE = %d\n", 50000);

    start = clock ();
    read_file_into_array (sorting_array, 50000, "m012345.txt");     //FILE READ
    now = clock ();
	elapsed_time_read = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent reading elements from input file 3 into RAM: %6.4fs\n", elapsed_time_read);

	start = clock ();
    insertion_sort (sorting_array, 50000);       //SORT
    now = clock ();
	elapsed_time_sort = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent sorting elements in RAM: %6.4fs\n", elapsed_time_sort);

    start = clock ();
    write_array_to_file (sorting_array, 50000, "insertion_sorted3.txt");   //FILE WRITE
    now = clock ();
	elapsed_time_write = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tTime spent writing elements in RAM to output file 3: %6.4fs\n", elapsed_time_write);

	printf("\tTOTAL FILE I/O TIME = %6.4fs\n", elapsed_time_read + elapsed_time_write);
	/*******END OF FILE 3 PROCESSING*******/

    //printf("\t");

    PrintStringWithDelay(end_op, 50);

	getch();
	return 0;
}

void insertion_sort (int *array, int array_size)
{
    int i, j;

    for (i = 1; i < array_size; i++) {
        for (j = i; j >= 1; j--) {
            if (array[j] >= array[j-1]) break;
            swap(&array[j], &array[j-1]);
        }
    }
}


void ReadSortWrite (char *InputFile, char *OutputFile, int FileSize)
{
    int sorting_array[MAX] = { 0 };
	clock_t start, now;
	double elapsed_time_read, elapsed_time_write, elapsed_time_sort;


    printf("\n\n\n");
	printf("\tPROCESSING FILE \"%s\", SIZE = %d\n", InputFile, FileSize);

    start = clock ();
    read_file_into_array (sorting_array, FileSize, InputFile);     //FILE READ
    now = clock ();
	elapsed_time_read = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tReading elements from input file \"%s\" into RAM: %6.4fs\n", InputFile, elapsed_time_read);

	start = clock ();
    insertion_sort (sorting_array, FileSize);       //SORT
    now = clock ();
	elapsed_time_sort = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tSorting elements in RAM: %6.4fs\n", elapsed_time_sort);

    start = clock ();
    write_array_to_file (sorting_array, FileSize, OutputFile);   //FILE WRITE
    now = clock ();
	elapsed_time_write = (double)(now - start) / CLOCKS_PER_SEC;
	printf("\tWriting elements in RAM to output file \"%s\": %6.4fs\n", OutputFile, elapsed_time_write);

	printf("\tTOTAL FILE I/O TIME = %6.4fs\n", elapsed_time_read + elapsed_time_write);
}


void PrintStringWithDelay (char *String, int Delay)
{
    int i;

    for (i = 0; i < strlen (String); i++) {
            printf("%c", String[i]);
            sleep(Delay);
    }
}
